#ifndef DIALOG_GH_H
#define DIALOG_GH_H

#include <QDialog>
#include <QSqlQuery>
#include <QTableWidgetItem>
#include <QLibrary>
#include <QPrinter>
#include <QPainter>
#include <QTimer>
#include <QMap>
#include "threadyb.h"
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkInterface>
#include <QNetworkRequest>
#include <QJsonObject>
#include <QJsonArray>

namespace Ui {
class Dialog_gh;
}

class Dialog_gh : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog_gh(QWidget *parent = nullptr);
    ~Dialog_gh();

    bool findCardReader();
    void iniCardReader(int pt);
    void printOut(QStringList p);   //print out regisger bill
    void readGhf(QString);  //检索当前挂号类别挂号费
    void save(QStringList);    //保存当前挂号记录
    void saveSI();   //保存医保相关记录， fin_ipr_siinmaininfo
    bool isValid(); //当前数据状态是否有效
    //bool checkYb(); //核查医保账户状态
    QString ybGh(); //提交医保挂号信息
    //bool checkGhtp();
    void setState(QString); //设置界面工作模式
    void getRegRec(QString s, QString n1, QString n2); //读身份证或医保卡后检索挂号信息
    void selectDoctor();    //显示出诊医生，做出默认选择
    void putBaseIn( QJsonObject &in, QString code);

    bool verifyIdCard(const char* idCard);// 身份证号有效性校验

public slots:
    void leBzhRet();
    void leSfzhRet();
    void twDpList_Clicked(QTableWidgetItem * itm);
    void windClose();
    void pbDsfzClicked();
    void pbYbdkClicked();
    void pbYbsmClicked();
    void pbGhClicked();
    void readRetVal(QString s);
    void on_cbbGhlb_currentTextChanged(QString);
    void on_pbClrScr_clicked();
    void onTimeOut();
    void on_pbRePrint_clicked();
    void on_twDocList_itemClicked(QTableWidgetItem * itm);
    void resizeEvent(QResizeEvent *event);
    void on_leSfzh_returnPressed();
    void on_pbExit_clicked();
    void on_pbGhtp_clicked();//换号
    void on_pbYjs_clicked();    //预结算

    //void handle4100(const int, const QString );  //
    void handleRet(const int, const QString, const QString );  //
    //void handleSignIn(const int, const QString );   //
    //void updateTitle();                             //更新界面

    void postRequestFinished(QNetworkReply *);

signals:
    void opThread(int, QString);    // 发送信号触发线程

private:
    Ui::Dialog_gh *ui;
    QSqlQuery *qrm, *qro, *qro1;
    QThread thread;
    ThreadYb *ybBusiness;
    QString statusMsg;
    QString sq;
    QString rName,rSex,rBirth,rId,rAge;//read from identityCardReader
    QLibrary lib;
    QPrinter *prt;
    QPainter *pnt;
    float ptScale;  //for printer, mm to point covnert.
    QStringList s ; //挂号票 票面内容
    QStringList ghlb_ghf;
    QStringList dept_name_code;
    QString ghType, ghCode, deptName, deptCode; //当前挂号类别，代码，科室，代码
    QString docName, docCode,docCodeYb; //选定医生姓名，代码,医生执业代码

    QString ghGbf, ghGhf, ghZcf; //工本费，挂号费，诊查费
    QString displayMode; //界面模式，new 或 retbzh
    bool ybEnabled; //医保可用标志
    QTimer * tm;    //界面显示刷新定时
    //QString tmString;   //界面定时消隐显示的字串

    typedef int __stdcall (*pCVR_InitComm)(int Port) ;//初始化连接;
    pCVR_InitComm CVR_InitComm;
    typedef int __stdcall (*pCVR_Authenticate)();//卡认证;
    pCVR_Authenticate CVR_Authenticate;
    typedef int __stdcall (*pCVR_Read_Content)(int active);//读卡操作。
    pCVR_Read_Content CVR_Read_Content;
    typedef int __stdcall (*pCVR_CloseComm)();//关闭连接;
    pCVR_CloseComm CVR_CloseComm;
    typedef int __stdcall (*pGetPeopleName)(char *strTmp, int *strLen);//姓名
    pGetPeopleName GetPeopleName;
    typedef int __stdcall (*pGetPeopleSex)(char *strTmp, int *strLen);//性别
    pGetPeopleSex GetPeopleSex;
    typedef int __stdcall (*pGetPeopleBirthday)(char *strTmp, int *strLen);//出生日期
    pGetPeopleBirthday GetPeopleBirthday;
    typedef int __stdcall (*pGetPeopleIDCode)(char *strTmp, int *strLen);//身份证号
    pGetPeopleIDCode GetPeopleIDCode;
    typedef int __stdcall (*pGetSexCode)(unsigned char * sexData, int * pLen);
    pGetSexCode GetSexCode;

//    typedef __stdcall int(*pINIT)(char* );
//    pINIT INIT;
//    typedef __stdcall int(*pBUSINESS)(char* ,char*);
//    pBUSINESS BUSINESS;

    bool haveCardReader,cardReaded; //身份证读卡可用，身份证读卡成功
    //QString yb; //医保状态，“card” ：读卡， “code” ： 扫码， “” ： 未使用医保
    QMap<QString,QString> mpPerson;//医保人员读卡返回信息

    QJsonObject personBaseinfo; //1101
    QJsonObject personInsuinfo; //1101
    QJsonObject personIdetinfo; //1101
    QJsonObject ret2201;
    QJsonObject ret2001;
    QJsonObject ret2206setlinfo;
    QJsonObject ret2206setldetail;
    QJsonObject ret2207setlinfo;
    QJsonObject ret2207setldetail;
    QJsonArray ret2204;
    QJsonObject retScan;

    QString ghNl;   //年龄
    QString ghNldw; //年龄单位
    QString regRowid, regClinicCode, regPact;   //挂号主表rowid, clinic_code, 挂号方式。

    int dpRows, dpColumns, docRows, docColumns; //诊室列表，医生列表 行列数
    bool initialed; //界面初始化完成标志
    volatile bool businessFinish;    //医保交易完成标志
    QString businessRetStr1; //医保交易返回串
    QString businessRetStr2; //医保交易返回串
    int businessRetVal; //医保交易返回串

    QString cur_fph;//即将打印的发票号，兼作2204的收费批次号
    QString defaultDeptDiag;    //诊室默认诊断
    QString cardInfo,busiCardInfo;  //卡串,业务卡串
    volatile bool httpFinished; //http结束
    bool httpSuccess;   //http成功
    QString currentInfno; //当前交易编号
    QString mdtrt_cert_type; //就诊凭证类型,交易方式，01电子凭证，02身份证，03社保卡
    bool ybSigned ;
    QNetworkReply * rpl ;
    QNetworkRequest request;
    //QNetworkAccessManager* naManager;
    QString msgid2207;
    QString currentMsgid;   //当前msgid值，用于保存日志

};

#endif // DIALOG_GH_H
